using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using Utils;
using Random = UnityEngine.Random;

public class FlowerBlueS : Monster
{
    [FormerlySerializedAs("bullet")] public GameObject bulletPrefab;
    public float bulletSpeed = 1.0f;

    private GameObject manaCube, goldCoin;
    private Transform pos;

    private void Awake()
    {
        roomCtr = transform.parent.parent.GetComponent<RoomController>();
        pos = UITool.GetComponent<Transform>("Pos", gameObject);

        graveObj = UITool.GetChildGameObject("Grave", transform.parent.parent.gameObject);
        manaCube = UITool.GetChildGameObject("ManaCube", gameObject);
        goldCoin = UITool.GetChildGameObject("GoldCoin", gameObject);

        animator = GetComponent<Animator>();
        collider2D = GetComponent<BoxCollider2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void OnEnable()
    {
        targetPosition = GameController.Instance().playerCtr.transform;
    }

    private void Update()
    {
        switch (monsterState)
        {
            case MonsterState.Idle:
                Idle();
                break;
            case MonsterState.Attack:
                Attack();
                break;
            case MonsterState.Die:
                Die();
                break;
            default:
                break;
        }
    }

    public override void Idle()
    {
        animator.SetBool("Idle", true);
    }

    public override void Stroll()
    {
    }

    public override void Tracking()
    {
    }

    public override void Attack()
    {
        if (roomCtr.inFightState)
        {
            animator.SetBool("Idle", false);
        }
    }

    public override void Die()
    {
        animator.SetBool("isDie", true);
        animator.SetBool("Idle", true);
        collider2D.enabled = false;
        if (GameController.Instance().archiveCtr.TempLevelData.gameType == GameType.Dungeon)
        {
            transform.SetParent(graveObj.transform);
        }

        DropActive();
        // GameController.Instance().weaponCtr.Weapon.ResetForward();
    }

    public override void BeAttacked()
    {
        if (hp <= 0)
        {
            monsterState = MonsterState.Die;
            GameController.Instance().archiveCtr.TempLevelData.killed++;
            if (isBoss)
            {
                GameController.Instance().panelCtr.RemoveBossPanel();
            }
        }
        else
        {
            animator.Play("BeAttacked");
        }
    }

    private void DropActive()
    {
        if (manaCube)
        {
            manaCube.SetActive(true);
        }

        int i = Random.Range(0, 5);
        if (i < 3) return;
        else
        {
            if (goldCoin)
            {
                goldCoin.SetActive(true);
                goldCoin.GetComponent<GoldCoin>().value = Random.Range(3, 6);
            }
        }
    }

    private void Shot()
    {
        if (monsterState != MonsterState.Die && roomCtr.inFightState)
        {
            float z = 0;
            for (int i = 1; i <= 12; i++)
            {
                // GameObject go = Instantiate(bulletPrefab, pos.position, pos.rotation);
                GameObject go = GameController.Instance().ObjectCtr.HollowCircleBullet();
                go.transform.position = pos.position;
                go.transform.rotation = pos.rotation;
                go.tag = transform.tag;
                go.SetActive(true);
                go.GetComponent<Bullet>().InitData(atk, bulletSpeed);
                pos.rotation = Quaternion.Euler(0, 0, z);
                z += 30;
            }
        }
    }
}